package dev.ece.util.tools;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

/**
 * 
 * 说明：缓冲创建器及操作
 *
 * BufferBuilder.java
 *
 * @author DEVILIVED
 * 
 * 版本信息 v1.0
 * 
 * 2013-5-27
 *
 * 修改历史 
 *
 * Copyright 2013, DEVILIVED Inc.
 */
public class BufferBuilder {

	public static final DoubleBuffer buildDoubleBuffer(int size) {
		DoubleBuffer d_buffer = null;
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		d_buffer = b_buffer.asDoubleBuffer();
		return d_buffer;
	}
	
	public static final DoubleBuffer buildDoubleBuffer(double[] buffer) {
		DoubleBuffer d_buffer = buildDoubleBuffer(buffer.length * 8);
		d_buffer.put(buffer);
		d_buffer.rewind();
		return d_buffer;
	}
	
	public static final FloatBuffer buildFloatBuffer(int size) {
		FloatBuffer f_buffer = null;
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		f_buffer = b_buffer.asFloatBuffer();
		return f_buffer;
	}
	
	public static final FloatBuffer buildFloatBuffer(float[] buffer) {
		FloatBuffer f_buffer = buildFloatBuffer(buffer.length * 4);
		f_buffer.put(buffer);
		f_buffer.rewind();
		return f_buffer;
	}
	
	public static final LongBuffer buildLongBuffer(long[] buffer) {
		LongBuffer l_buffer = buildLongBuffer(buffer.length * 8);
		l_buffer.put(buffer);
		l_buffer.rewind();
		return l_buffer;
	}
	
	public static final IntBuffer buildIntBuffer(int[] buffer) {
		IntBuffer i_buffer = buildIntBuffer(buffer.length * 4);
		i_buffer.put(buffer);
		i_buffer.rewind();
		return i_buffer;
	}
	
	public static final ShortBuffer buildShortBuffer(short[] buffer) {
		ShortBuffer s_buffer = buildShortBuffer(buffer.length * 2);
		s_buffer.put(buffer);
		s_buffer.rewind();
		return s_buffer;
	}
	
	public static final IntBuffer buildIntBuffer(int size) {
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		return b_buffer.asIntBuffer();
	}
	
	public static final ShortBuffer buildShortBuffer(int size) {
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		return b_buffer.asShortBuffer();
	}
	
	public static final LongBuffer buildLongBuffer(int size) {
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		return b_buffer.asLongBuffer();
	}
	
	public static final ByteBuffer buildByteBuffer(int size) {
		ByteBuffer b_buffer = ByteBuffer.allocateDirect(size);
		b_buffer.order(ByteOrder.nativeOrder());
		return b_buffer;
	}
	
}
